
log using "$rootpath/log/Table_8_Results.log", replace
eststo clear
use "$rootpath/processed_data/Estimation_Data.dta", clear

sort naics 
gen log_all=asinh(All/1000)
egen industry_mean_dam=mean(log_all), by(naics)

gen avg_old_code_probwh=avg_old_code_prob*(log_all/(industry_mean_dam+1)) 
egen industry_mean=mean(avg_old_code_probwh), by(naics year)

gen treat_diff=(treatXpost - industry_mean)*post

gen treat_diffXtreatXpost = treatXpost*treat_diff

sort facility_id year
egen num_obs2=nvals(year), by(facility_id)
//facility-level se
reghdfe  log_damage55  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1&naics>300000&naics<400000, absorb(facility_id n5#c.year) vce(cluster facility_id)
estimates store ddd_1_damage

//state-level se
reghdfe  log_damage55  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1&naics>300000&naics<400000, absorb(facility_id n5#c.year) vce(cluster state_number)
estimates store ddd_1_damageSE
//facility-level se
reghdfe  log_emissions4  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1&naics>300000&naics<400000, absorb(facility_id n5#c.year) vce(cluster facility_id)
estimates store ddd_1_emission

//state-level se
reghdfe  log_emissions4  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1&naics>300000&naics<400000, absorb(facility_id n5#c.year) vce(cluster state_number)
estimates store ddd_1_emissionSE

use"$rootpath/processed_data/Estimation_Data.dta", clear
gen temp_id=facility_id if watch_list==1
sort state
egen state_wls=nvals(temp_id), by(state)
egen count_state_wls=max(state_wls), by(state)
replace count_state_wls=0 if count_state_wls==.
drop temp_id
gen temp_id=facility_id if post==0
sort state
egen state_plant_count=nvals(temp_id), by(state)
egen count_state_plants=max(state_plant_count), by(state)
  
gen treat_diff=(count_state_wls/count_state_plants)*post

gen treat_diffXtreatXpost = treatXpost*treat_diff

//Note that clustering at state_number#naics is interesting. 
//facility-level se
reghdfe  log_damage55  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store ddd_2_damage

//state-level se
reghdfe  log_damage55  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster state_number)
estimates store ddd_2_damageSE
///This is emissions
//facility-level se
reghdfe  log_emissions4  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)

estimates store ddd_2_emission

//state-level se
reghdfe  log_emissions4  treat_diffXtreatXpost treatXpost treat_diff if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster state_number)
estimates store ddd_2_emissionSE

esttab ddd_1_damage ddd_2_damage ddd_1_emission ddd_2_emission using "$rootpath/table_graph/DDD_TBL8.tex",  star( * 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) keep(treat_diffXtreatXpost treatXpost treat_diff _cons) scalars(N F) ar2 replace 
esttab ddd_1_damageSE ddd_2_damageSE ddd_1_emissionSE ddd_2_emissionSE using "$rootpath/table_graph/DDD_TBL8SE.tex",  star( * 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) keep(treat_diffXtreatXpost treatXpost treat_diff _cons) scalars(N F) ar2 replace 
log close
